package com.xaicode.template.batch.job.normal;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author beborn
 * @date 2021/12/30 0030 17:22
 */
@Component
public class FlowJobDemo {

    @Resource
    private JobBuilderFactory jobBuilderFactory;

    @Resource
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job flowJob() {
        return jobBuilderFactory.get("flow")
                .start(flow())
                .next(step3())
                .end()
                .build();
    }

    private Flow flow() {
        return new FlowBuilder<Flow>("flow")
                .start(step1())
                .next(step2())
                .build();
    }

    private Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet(JobHelper.tasklet("FlowJob - 1", RepeatStatus.FINISHED))
                .build();
    }

    private Step step2() {
        return stepBuilderFactory.get("step2")
                .tasklet(JobHelper.tasklet("FlowJob - 2", RepeatStatus.FINISHED))
                .build();
    }

    private Step step3() {
        return stepBuilderFactory.get("step3")
                .tasklet(JobHelper.tasklet("FlowJob - 3", RepeatStatus.FINISHED))
                .build();
    }


}
